#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        int n = s.size();
        unordered_set<string> hashi;
        for (auto& e : wordDict) {
            hashi.insert(e);
        }
        vector<bool> dp(n + 1);
        dp[0] = true;
        s = ' ' + s;
        for (int i = 1; i <= n; i++) {
            for (int j = i; j >= 1; j--)
            {
                if (dp[j - 1] && hashi.count(s.substr(j, i - j + 1))) {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[n];
    }
};
